www.gusucode.com > C++ 实现的共生矩阵-源码程序 > C++ 实现的共生矩阵-源码程序/code/aa.cpp
//Download by http://www.NewXing.com /*图像理解与分析中 灰度共生矩阵算法 周一早上图像理解与分析课上,朱启疆老师讲了灰度共生矩阵算法,回去后我编程实现了这个算法。 内容如下: 共有 matrix.cpp d_matrix.h d_exept.h mat.txt 四个文件 */ //matrix.cpp /* Visual C++ 6.0 matrix designed by bfly */ #include <iostream> #include <fstream> #include <cstdlib> #include <cmath> #include <vector> #include "d_matrix.h" template <typename T> void outputmat(const matrix<T>& mat); template <typename T> int classifymat(const matrix<T>& mat); template <typename T> void transformmat(const matrix<T>& formermat, matrix<T>& lattermat); template <typename T> void probablitymat(const matrix<T>& mat,matrix<T>& probmat); template <typename T> void typicalarguement(const matrix<T>& mat,const matrix<T>& probmat); using namespace std; int main() { //input matrix matrix<float> initMat; int numRows, numCols; int i, j; ifstream fin("mat.txt"); if(!fin) { cerr << "Cannot open 'mat.txt'" << endl; exit(1); } fin >> numRows >> numCols; initMat.resize(numRows, numCols); for(i = 0; i < numRows; i++) { for(j = 0; j < numCols; j++) { fin >> initMat[i][j]; } } //transform matrix to tempMat int counter=classifymat(initMat); matrix<float> tempMat; tempMat.resize(counter, counter); transformmat(initMat, tempMat); outputmat(tempMat); //transform matrix to probMat matrix<float> probMat; probMat.resize(counter, counter); probablitymat(tempMat, probMat); outputmat(probMat); cout << endl; //output the typicalarguements typicalarguement(tempMat, probMat); return 0; } //outputmat matrix functrion template <typename T> void outputmat(const matrix<T>& mat) { int i, j; for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { cout << mat[i][j] << " "; } cout << endl; } } //classifymat matrix function template <typename T> int classifymat(const matrix<T>& mat) { vector<T> memoryval; memoryval.push_back(mat[0][0]); int counter=1; bool flag = false; int i, j; for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { for(int m = 0; m < memoryval.size(); m++) { if(mat[i][j] == memoryval[m]) flag = true; } if(!flag) { memoryval.push_back(mat[i][j]); counter++; } flag = false; } } return counter; } //transformmat matrix function template <typename T> void transformmat(const matrix<T>& formermat, matrix<T>& lattermat) { cout << "Plesase enter a and b: "; int a, b; int matval = 0; cin >> a >> b; int i, j, m, n; for(i = 0; i < lattermat.rows(); i++) { for(j = 0; j < lattermat.cols(); j++) { for(m = 0; m < formermat.rows(); m++) { for(n = 0; n < formermat.cols(); n++) { if(formermat[m][n]==i) { if(((m+a) < formermat.rows()) && ((n+b) < formermat.cols())) { if(formermat[m+a][n+b] == j) { matval++; } } } } } lattermat[i][j]=matval; matval=0; } } } //probablitymat matrix function template <typename T> void probablitymat(const matrix<T>& mat,matrix<T>& probmat) { T sum = T(); int i, j; for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { sum += mat[i][j]; } } for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { probmat[i][j] = mat[i][j]/sum; } cout << endl; } } //typicalarguements template <typename T> void typicalarguement(const matrix<T>& mat,const matrix<T>& probmat) { T e = T(), H = T(), C = T(),I = T(), mean = T(), stdvar = T(), sum = T(), var = T(); T M = T(); int i, j; //typicalargument e for(i = 0; i < probmat.rows(); i++) { for(j = 0; j < probmat.cols(); j++) { e += probmat[i][j]*probmat[i][j]; } } //typicalargument H for(i = 0; i < probmat.rows(); i++) { for(j = 0; j < probmat.cols(); j++) { H += probmat[i][j]*log(probmat[i][j])/log(10.0); } } H = -H; //typicalargument sum for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { sum += mat[i][j]; } } //typicalargument mean mean = sum/(mat.rows()*mat.cols()); //typicalargument var for(i = 0; i < mat.rows(); i++) { for(j = 0; j < mat.cols(); j++) { var += (mat[i][j]-mean)*(mat[i][j]-mean); } } //typicalargument stdvar stdvar=sqrt(var); //typicalargument C for(i = 0; i < probmat.rows(); i++) { for(j = 0; j < probmat.cols(); j++) { C += (i - mean)*(j - mean)*probmat[i][j]; } } C /= (stdvar*stdvar); //typicalargument M for(i = 0; i < probmat.rows(); i++) { for(j = 0; j < probmat.cols(); j++) { M += (probmat[i][j]/ (1 + (i - j)*(i - j))); } } //typicalargument I for(i = 0; i < probmat.rows(); i++) { for(j = 0; j < probmat.cols(); j++) { I += (i - j)*(i -j)*probmat[i][j]; } } //output typicalarguments cout<<" 能量e = "<< e <<endl; cout<<" 熵H = "<< H <<endl; cout<<" 相关性C = "<< C <<endl; cout<<" 局部均匀性M = "<< M <<endl; cout<<" 惯性I = "<< I <<endl; } //剩下内容请看下一页的图像理解与分析中灰度共生矩阵算法2